package com.home.andromo;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter {
	private Context mContext;
	private List mListAppInfo;
	private PackageManager mPackManager;

	public AppInfoAdapter(Context c, List list, PackageManager pm) {
		mContext = c;
		mListAppInfo = list;
		mPackManager = pm;
	}

	@Override
	public int getCount() {
		return mListAppInfo.size();
	}

	@Override
	public Object getItem(int position) {
		return mListAppInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// get the selected entry
		Application app = (Application) mListAppInfo.get(position);
		
		ApplicationInfo entry = (ApplicationInfo) app.getApp();

		// reference to convertView
		View v = convertView;

		// inflate new layout if null
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.layout_appinfo, null);
		}

		// load controls from layout resources
		// ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
		//TextView tvAppName = (TextView) v.findViewById(R.id.tvName);

		// set data to display
		// ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
		//tvAppName.setText(entry.packageName);
		ImageView icono = (ImageView) v.findViewById(R.id.ivIcon);
		TextView appName = (TextView) v.findViewById(R.id.appName);
		
		  Bitmap icono_app = app.cargarImagen(); 
			if(icono_app != null){
			icono.setImageBitmap(icono_app);
			appName.setText("");
			}else{
				ImageDownloader downloader = new ImageDownloader();
				 downloader.download(app.getApp().packageName, icono);
				 icono.setImageResource(R.drawable.app_no_image);
				 appName.setText(entry.loadLabel(mPackManager));
			}
		  
		

		// return view
		return v;
	}

	


}
